﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using MillerApp.DataContract.Common;
using MillerApp.DataContract.MillerDB;
using MillerApp.DataAccess.MillerDB;
using System.Reflection;
using MillerApp.Stock.Common;
using MillerApp.CommonLib.Utility;
using MillerApp.UserControls;
using MillerApp.Common;

namespace MillerApp.Stock
{
    public partial class MainForm : MainFormBase
    {
        public MainForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 当前用户
        /// </summary>
        public UserEntity CurrentUser { get; set; }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //设置居中显示
            WinEffect.SetMid(this);
            //初始化菜单
            int recordCount = 0;
            List<CommontypeEntity> list = CommontypeManager.GetCommontypesByCond("and typename='系统菜单'", string.Empty, 0, int.MaxValue, out recordCount);
            ToolStripMenuItem parentMenuItem = null;
            this.BuildMenu(ref list, string.Empty, ref parentMenuItem);
        }
        /// <summary>
        /// 使用递归构造菜单
        /// </summary>
        /// <param name="menulist">所有的菜单列表</param>
        /// <param name="parentCode">上一级菜单编号</param>
        /// <param name="parentMenuItem">上一级菜单</param>
        private void BuildMenu(ref  List<CommontypeEntity> menulist, string parentCode, ref ToolStripMenuItem parentMenuItem)
        {
            //获取所有的下一级菜单
            var a = from b in menulist
                    where b.parentcode == parentCode
                    orderby b.itemcode
                    select b;
            //如果没有下级菜单，则直接退出此方法
            if (a == null || a.Count<CommontypeEntity>() <= 0)
            {
                return;
            }
            //从LINQ中获取所有子菜单
            List<CommontypeEntity> selectItems = a.ToList<CommontypeEntity>();
            //遍历所有子菜单，增加到父菜单下，然后附加按钮点击动作
            foreach (var item in selectItems)
            {
                ToolStripMenuItem menuItem = new ToolStripMenuItem();
                menuItem.Name = item.itemcode + "_" + item.itemname;//命名
                menuItem.Text = item.itemname;//显示文字
                menuItem.Tag = item.res1;//res1存储的是Form的TypeName路径，需要铜鼓
                if (string.IsNullOrEmpty(item.res1) || string.IsNullOrEmpty(item.res1.Trim()))
                {
                    //如果没有配置窗体路径，则不处理
                }
                else
                {
                    //配置了窗体路径，则附加点击事件
                    menuItem.Click += delegate(object sender, EventArgs arg)
                    {
                        ToolStripMenuItem menuClick = sender as ToolStripMenuItem;
                        //获取TypeName
                        string className = menuClick.Tag.ToString();
                        Form[] childrenWin = this.MdiChildren;
                        //获取当前窗体的所有子窗体，如果已经开启了，则直接激活窗口
                        foreach (var childItemWin in childrenWin)
                        {
                            if (childItemWin.GetType().FullName == className)
                            {
                                childItemWin.Activate();
                                return;
                            }
                        }
                        //如果没有开启，则创建新窗体并激活置前
                        Assembly asm = Assembly.GetExecutingAssembly();
                        Type t = asm.GetType(className, false, true);
                        if (t != null)
                        {
                            object obj = Activator.CreateInstance(t);
                            if (obj != null && obj is Form)
                            {
                                Form subForm = obj as Form;
                                subForm.MdiParent = this;
                                subForm.Show();
                                subForm.Activate();
                            }
                        }
                    };
                }
                //设置菜单控件的大小，如果是 一级菜单 ，则设置宽度为45,高度为22,不是的话则设置宽度为152，高度为22
                if (string.IsNullOrEmpty(parentCode))
                {
                    menuItem.Size = new System.Drawing.Size(45, 22);
                    this.menuStrip.Items.Add(menuItem);
                }
                else
                {
                    menuItem.Size = new System.Drawing.Size(152, 22);
                    parentMenuItem.DropDownItems.Add(menuItem);
                }
                //遍历下级菜单
                BuildMenu(ref menulist, item.itemcode, ref menuItem);
            }
        }
    }
}
